<template>
	<view class="wrap">
		<view class="box">
			<view style="text-align: center;font-size: 33rpx;font-weight: bold;color: #20CBD4;">微信公众号授权登陆</view>
			<view style="margin: 50rpx; width: 200rpx;height: 160rpx;">
				<image style="width: 100%;height: 100%;" src="/static/weixinshouquan.png"></image>
			</view>
			<view><u-button @click="shouquan" :custom-style="{background:'#20cbd4',color:'#fff'}" size="medium">点击授权</u-button></view>
		</view>
		<hoverMenu></hoverMenu>
	</view>
</template>

<script>
	import hoverMenu from "@/components/public/sa-hover-menu.vue";
	export default {
		components: {
			hoverMenu
		},
		data() {
			return {
				goodsId:''
			}
		},
		onLoad(options) {
			this.goodsId=options.state;
			this.postCode(options.code);
		},
		methods: {
			floatTouchClick(index){
				this.$uniApi.floatTouch(index);
			},
			shouquan(){
				// 获取URL 上code
				const code = this.getUrlParam('code')
				// 判断是否存在code
				if(code == null || code == '') {
					if(uni.getStorageSync("openidGzh")==null||uni.getStorageSync("openidGzh")==""){
						// 重新获取code
						window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc2141b1a0c574f03&redirect_uri=https://wap.yunxingzhan.com/h5/pages/views/authorization&response_type=code&scope=snsapi_userinfo&state='+this.goodsId+'#wechat_redirect'
						//window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc2141b1a0c574f03&redirect_uri=https://testwap.yunxingzhan.com/h5/pages/views/authorization&response_type=code&scope=snsapi_userinfo&state='+this.goodsId+'#wechat_redirect'
					}
					
				} else {
					this.postCode(code);
				}
			},
			// 解析URL 参数
			getUrlParam(name) {
				let reg = new RegExp('(^|&)'+ name + '=([^&]*)(&|$)')
				let r = window.location.search.substr(1).match(reg)
				if(r!=null){
					return unescape(r[2])
				} 
				return null
			},
			// 发送code 获取信息
			postCode(code) {
				var _this=this;
				this.$u.api.wxchat_get_openid({code:code}).then(res=>{
					if(res.code==200){
						uni.setStorageSync('openidGzh', res.data.openid);
						uni.navigateTo({
							url:"/goods/goodsDetail?goods_id="+_this.goodsId,
							success(){
								_this.$u.toast("授权成功")
							}
						})
					}
				})
			},
		}
	}
	
</script>

<style>
	page{
		font-family: PingFang SC;
		background: #20cbd4;
	}
</style>
<style lang="scss" scoped>
	.wrap {
		height: 80vh;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.box{
		width: 500rpx;
		border-radius: 20rpx;
		padding: 40rpx;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
</style>
